<?php

namespace lxs\tools;

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
use Illuminate\Support\Facades\Redis;
use lxs\api\ApiResponse;

class WeChatApplet
{
    private static $appId = "wx2d59d659ccc8e65b";

    /**
     * 获取小程序码
     */
    public static function getAppletCode($id, $code, $page, $envVersion = 'trial', $checkPath = false, int $width = 1280)
    {
        $client = new Client();

        // 获取二维码文件
        $res = $client->request("post", "http://lxs_api.yangli520.cn/v1/applet_code/generator", [
            RequestOptions::HEADERS => [
                'appId' => config('lxs_api.appId'),
                'appSecret' => config('lxs_api.appSecret')
            ],
            "json" => [
                "appId" => self::$appId,
                "platform" => "wechatApplet",
                "scene" => "id={$id}&code={$code}",
                "envVersion"  => $envVersion,
                "page" => $page,
                "checkPath" => $checkPath,
                "width" => $width,
                'ossPlatform' => 'aLiYun',
                'ossParam' => [
                    "accessKeyId" => "LTAI5tNitQwFF9gTKEzaXL7q",
                    "apiHost" => "oss-cn-shenzhen.aliyuncs.com",
                    "bucket"  => "guzhong1",
                    "savePath" => "gzqk/applet_code/" . $code . ".png",
                ],
            ],
        ]);

        if (json_decode((string)$res->getBody(), true)['code'] != 200) {
            return (string)$res->getBody();
        }

        return json_decode((string)$res->getBody(), true)['data']['accessUrl'];
    }
}
